<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在线文件编辑</title>
<script type="text/javascript" src="<%=basePath %>fileManager/js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="<%=basePath %>ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="<%=basePath %>ckfinder/ckfinder.js"></script>
<script src="<%=basePath %>fileManager/jquery.alerts/js/jquery.alerts.js" type="text/javascript"></script>

<link href="<%=basePath %>fileManager/jquery.alerts/css/jquery.alerts.css" rel="stylesheet"
	type="text/css" />

<script type="text/javascript">
	var MANAGER_SERVLET = "<%=basePath %>OnlineFileManagerServlet"; //指定管理功能的Servlet的路径
	var fileName = getUrlParam("fileName");
	var dir = getUrlParam("dir");
	var suffix = "";
	var suffixIdx = fileName.lastIndexOf(".");
	if(suffixIdx>=0) {
		suffix = fileName.slice(suffixIdx);
	}
	var virualSuffixs = ['.html','.htm','.jsp','.php','.aspx','.asp'];
	var ckeMode = $.inArray(suffix,virualSuffixs)>=0 ? 'wysiwyg' : 'source';
	var config = {
			enterMode : CKEDITOR.ENTER_BR,
			startupMode : ckeMode,
			extraPlugins: 'stylesheetparser,savebtn',
			filebrowserBrowseUrl : '<%=basePath %>fileManager/fileManager.html?rootFolder=/userFiles',
			filebrowserImageBrowseUrl : '<%=basePath %>fileManager/fileManager.html?rootFolder=/userFiles'
	}
	if(ckeMode == 'source') {
		config.toolbar = [['savebtn']]
	}else {
		config.toolbar = [
			{ name: 'document', groups: [ 'mode', 'document', 'doctools' ], items: [ 'Source', '-', 'savebtn', 'NewPage', 'Preview', 'Print', '-', 'Templates' ] },
			{ name: 'clipboard', groups: [ 'clipboard', 'undo' ], items: [ 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo' ] },
			{ name: 'editing', groups: [ 'find', 'selection', 'spellchecker' ], items: [ 'Find', 'Replace', '-', 'SelectAll', '-', 'Scayt' ] },
			{ name: 'forms', items: [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField' ] },
			'/',
			{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ], items: [ 'Bold', 'Italic', 'Underline', 'Strike', 'Subscript', 'Superscript', '-', 'RemoveFormat' ] },
			{ name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ], items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote', 'CreateDiv', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', '-', 'BidiLtr', 'BidiRtl' ] },
			{ name: 'links', items: [ 'Link', 'Unlink', 'Anchor' ] },
			{ name: 'insert', items: [ 'Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak', 'Iframe' ] },
			'/',
			{ name: 'styles', items: [ 'Styles', 'Format', 'Font', 'FontSize' ] },
			{ name: 'colors', items: [ 'TextColor', 'BGColor' ] },
			{ name: 'tools', items: [ 'Maximize', 'ShowBlocks' ] },
			{ name: 'others', items: [ '-' ] },
			{ name: 'about', items: [ 'About' ] }
		];

		config.toolbarGroups = [
			{ name: 'document', groups: [ 'mode', 'document', 'doctools' ] },
			{ name: 'clipboard', groups: [ 'clipboard', 'undo' ] },
			{ name: 'editing', groups: [ 'find', 'selection', 'spellchecker' ] },
			{ name: 'forms' },
			'/',
			{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ] },
			{ name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ] },
			{ name: 'links' },
			{ name: 'insert' },
			'/',
			{ name: 'styles' },
			{ name: 'colors' },
			{ name: 'tools' },
			{ name: 'others' },
			{ name: 'about' }
		];
	}
	var editor;
	var bodyHeight = 0;
	$(function() {
		if(typeof(window.innerWidth) == 'number') //Non-IE
		  	bodyHeight = window.innerHeight;
		else if(document.documentElement && document.documentElement.clientHeight) //IE 6+ 'stds compliant mode'
		    bodyHeight = document.documentElement.clientHeight;
		else if( document.body && document.body.clientHeight) { //IE 4 compatible
		    bodyHeight = document.body.clientHeight;
		}
		editor = CKEDITOR.replace('editor',config);
		editor.on( 'instanceReady', function( event ){
			var editor = event.editor;
			readFile(editor);
			editor.resize("100%",bodyHeight - 40);
			if(ckeMode=='source') {
				//editor.
			}
		}, null, null, 9999 );
		editor.on('save',function(event){
			event.cancel();
			var editor = event.editor;
			editor.updateElement();
			$.post(MANAGER_SERVLET, {
				command : "saveFile",
				file : fileName,
				directory : dir,
				fileContent : editor.getData()
			}, function(data) {
				if (data == null || data['success'] != true) {
					jAlert('error', data.msg);
					return;
				}
				jAlert("info","保存成功","提示");
			}, "json");
		});
	});

	/**
	 * //获取request参数
	 */
	function getUrlParam(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null)
			return decodeURI(r[2]);
		return null;
	}

	function readFile(editor) {
		$.post(MANAGER_SERVLET, {
			command : "readFile",
			file : fileName,
			directory : dir
		}, function(data) {
			if (data == null || data['success'] != true) {
				jAlert('error', "加载文件内容失败");
				return;
			}
			editor.setData(data.fileContent);
		}, "json");
	}

</script>
</head>

<body>
	<textarea id="editor" name="editor"></textarea>
</body>
</html>